import { console } from 'node:inspector/promises';

import { useAccessStore } from '@vben/stores';

import { message } from 'ant-design-vue';
import { defineStore } from 'pinia';

import { getCaptChaImage, getUserInfoApi, userLogin } from '#/api';

// const accessStore = useAccessStore();
// const userStore = useUserStore();
// const router = useRouter();
export const useAuthstore = defineStore('auth', {
  state: () => ({
    loginLoading: false,
    captchaImage: {
      image: '',
      uuid: '',
    },
  }),
  actions: {
    async refreshCaptchaImage() {
      const {
        data: { img, uuid },
      } = await getCaptChaImage();
      this.captchaImage.image = `data:image/;base64,${img}`;
      this.captchaImage.uuid = uuid;
    },
    async authlogin(
      params: Parameters<typeof userLogin>[0],
      // onSuccess?: () => Promise<void> | void,
    ) {
      this.loginLoading = true;
      const {
        data: { code, token, msg },
      } = await userLogin(params);
      if (code === 200) {
        message.success('恭喜你，登录成功🎉');
        const accessStore = useAccessStore();
        accessStore.setAccessToken(token);
        const data = await getUserInfoApi();
        console.warn(data);
      } else if (code === 500) {
        message.error(msg);
        this.refreshCaptchaImage();
      }
      // if (token) {
      //   const accessStore = useAccessStore();
      //   accessStore.setAccessToken(token);
      //   const data = await getUserInfoApi();
      //   console.log('data:', data);
      // }
    },
    // async loginout() {},
    // async fetchUserInfo() {},
    $reset() {},
  },
});
